package com.xh.baselibrary.utils.net;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import org.jetbrains.annotations.NotNull;

import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Queue;

class LimitQueue<E> implements Queue<E> {
    private int limit;
    Queue<E> queue = new LinkedList<E>();
    public LimitQueue(int limit){
        this.limit = limit;
    }
    public Queue<E> getQueue(){
        return queue;
    }
    public int getLimit(){
        return limit;
    }
    @Override
    public int size() {
        return 0;
    }

    @Override
    public boolean isEmpty() {
        return queue.size()==0;
    }

    @Override
    public boolean contains(@Nullable @org.jetbrains.annotations.Nullable Object o) {
        return queue.contains(o);
    }

    @NotNull
    @Override
    public Iterator<E> iterator() {
        return queue.iterator();
    }

    @NotNull
    @Override
    public Object[] toArray() {
        return queue.toArray();
    }

    @NonNull
    @NotNull
    @Override
    public <T> T[] toArray(@NonNull @NotNull T[] a) {
        return queue.toArray(a);
    }

    @Override
    public boolean add(E e) {
        return queue.add(e);
    }

    @Override
    public boolean remove(@Nullable @org.jetbrains.annotations.Nullable Object o) {
        return queue.remove(o);
    }

    @Override
    public boolean containsAll(@NotNull Collection<?> c) {
        return queue.containsAll(c);
    }

    @Override
    public boolean addAll(@NonNull @NotNull Collection<? extends E> c) {
        return queue.addAll(c);
    }

    @Override
    public boolean removeAll(@NonNull @NotNull Collection<?> c) {
        return queue.removeAll(c);
    }

    @Override
    public boolean retainAll(@NonNull @NotNull Collection<?> c) {
        return queue.retainAll(c);
    }

    @Override
    public void clear() {
    queue.clear();
    }

    @Override
    public boolean offer(E e) {
        if (queue.size()>= limit){
            //如果超出长度，入队时，先出队
            queue.poll();
        }
        return queue.offer(e);
    }

    @Override
    public E remove() {
        return queue.remove();
    }

    @Nullable
    @org.jetbrains.annotations.Nullable
    @Override
    public E poll() {
        return queue.poll();
    }

    @Override
    public E element() {
        return queue.element();
    }

    @Nullable
    @org.jetbrains.annotations.Nullable
    @Override
    public E peek() {
        return queue.peek();
    }
}
